package model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Publicly accessible struct representing a player.
 * 
 */
public class PlayerModel {
	/** Contains the contents of the player's hand.
	 * Note that order matters for the hand. We may want to impose some sort on the cards in the hand, 
	 * and that sort is reflected in the model. 
	 */
	private ArrayList<Card> cards;
	/** The name of the player. */
	private String name;
	private HandOrdering order;
	public PlayerModel() {
		this.name = "";
		this.cards = new ArrayList<Card>();
		order = HandOrdering.UNORDERED;
		
		//Temporary testing code
		setOrder(HandOrdering.BYSUIT);
		//loadSomeRandomCards();
	}
	private void loadSomeRandomCards() {
		//TODO make sure jokers display properly
		addCard(new Card(15, Suit.JOKER));
		addCard(new Card((int)(1+Math.random()*13), Suit.SPADE));
		addCard(new Card((int)(1+Math.random()*13), Suit.DIAMOND));
		addCard(new Card((int)(1+Math.random()*13), Suit.CLUB));
		addCard(new Card((int)(1+Math.random()*13), Suit.SPADE));
		addCard(new Card((int)(1+Math.random()*13), Suit.HEART));
		addCard(new Card((int)(1+Math.random()*13), Suit.SPADE));
		addCard(new Card((int)(1+Math.random()*13), Suit.HEART));
		addCard(new Card(14, Suit.JOKER));
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public ArrayList<Card> getCards() {
		return cards;
	}
	
	public void addCard(Card c) {
		cards.add(c);
		//TODO optimize so we're not resorting after every card insertion
		resortHand();
	}
	
	public void removeCard(Card c) {
		cards.remove(c);
	}
	
	public HandOrdering getOrder() {
		return order;
	}
	public void setOrder(HandOrdering h) {
		order = h;
		resortHand();
	}
	public void resortHand() {
		if(order==HandOrdering.BYSUIT){
			Collections.sort(cards, new Comparator<Card>() {
				@Override
				public int compare(Card o1, Card o2) {
					return f(o1)-f(o2);
				}
				public int f(Card c) {
					if(c.isBigJoker()) 
						return 1;
					if(c.isSmallJoker()) 
						return 2;
					int ret = -1;
					if(c.suit==Suit.SPADE)
						ret=1000;
					else if(c.suit==Suit.HEART)
						ret=2000;
					else if(c.suit==Suit.DIAMOND)
						ret=3000;
					else if(c.suit==Suit.CLUB)
						ret=4000;
					if(c.value==1)
						ret+=1;
					else
						ret+=15-c.value;
					return ret;
				}
				
			});
		} else if(order==HandOrdering.BYVALUE) {
			Collections.sort(cards, new Comparator<Card>() {
				@Override
				public int compare(Card o1, Card o2) {
					return f(o1)-f(o2);
				}
				public int f(Card c) {
					if(c.isBigJoker()) 
						return 1;
					if(c.isSmallJoker()) 
						return 2;
					int ret = -1;
					if(c.suit==Suit.SPADE)
						ret=10;
					else if(c.suit==Suit.HEART)
						ret=20;
					else if(c.suit==Suit.DIAMOND)
						ret=30;
					else if(c.suit==Suit.CLUB)
						ret=40;
					if(c.value==1)
						ret+=1000;
					else
						ret+=1000*(15-c.value);
					return ret;
				}
				
			});
		}
	}
}
